import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_blocked_members/lf_blocked_members_logic.dart';
import 'package:largefriends/pages/lf_disable_account/lf_disable_account_logic.dart';
import 'package:largefriends/pages/lf_feedback/lf_feedback_logic.dart';
import 'package:largefriends/pages/lf_manage_access/lf_manage_access_logic.dart';
import 'package:largefriends/pages/lf_setting/lf_setting_logic.dart';
import 'package:largefriends/pages/lf_visibility/lf_visibility_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/category_widget.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';

import '../../config/lf_config.dart';
import '../../res/strings/lf_strings.dart';
import '../lf_browser/lf_browser_logic.dart';
import '../lf_change_email/lf_change_email_logic.dart';
import '../lf_change_password/lf_change_password_logic.dart';
import '../lf_suspend/lf_suspend_logic.dart';
import '../lf_verify/lf_verify_logic.dart';

class LfSettingPage extends StatefulWidget {
  const LfSettingPage({super.key});

  @override
  State<LfSettingPage> createState() => _LfSettingPageState();
}

class _LfSettingPageState extends State<LfSettingPage> {
  final logic = Get.put(LfSettingLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: lfAppBar(
          leadingCallback: () {
            Get.back();
          },
          title: strSettings.tr,
        ),
        body: ScrollConfiguration(
          behavior: LfScrollBehavior(),
          child: SingleChildScrollView(
            child: Padding(
              padding: EdgeInsets.only(left: 16.w, right: 16.w),
              child: Column(
                children: [
                  LfCategoryWidget(
                    title: strAccountSettings.tr,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 36.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    child: Text(
                                  strVerification.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                )),
                                verificationTip(),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            if (logic.authService.currentAccount!.verified ==
                                    1 ||
                                logic.authService.currentAccount!.verified ==
                                    2) {
                              return;
                            }
                            toVerifyPage();
                          },
                        ),
                        _divider(),
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 36.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  strPrivacyAndVisibility.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                ),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toVisibility();
                          },
                        ),
                        _divider(),
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 40.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    child: Text(
                                  strManageAccess.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                )),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toManageAccess();
                          },
                        ),
                        _divider(),
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 36.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    child: Text(
                                  strChangePassword.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                )),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toChangePassword();
                          },
                        ),
                        _divider(),
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 36.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    child: Text(
                                  strChangeEmail.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                )),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toChangeEmail();
                          },
                        ),
                      ],
                    ),
                  ),
                  LfCategoryWidget(
                    title: strNotifications.tr,
                    child: Column(
                      children: [
                        Container(
                          constraints: BoxConstraints(minHeight: 40.h),
                          alignment: Alignment.centerLeft,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                strMessages.tr,
                                style: lfTextStyle(
                                    sp: 15.sp, fontFamily: fontMedium),
                                overflow: TextOverflow.ellipsis,
                              ),
                              Obx(
                                () => Switch(
                                  value: logic.messageStatus.value == 1,
                                  activeColor: colorFF007F,
                                  onChanged: (value) {
                                    if (logic.messageStatus.value == 1) {
                                      logic.changeMessageStatus(0);
                                    } else {
                                      logic.changeMessageStatus(1);
                                    }
                                  },
                                ),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  LfCategoryWidget(
                    title: strBlockFeedback.tr,
                    child: Column(
                      children: [
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 40.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    child: Text(
                                  strBlockedMembers.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                )),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toBlockedMembers();
                          },
                        ),
                        _divider(),
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 40.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                  child: Text(
                                    strFeedback.tr,
                                    style: lfTextStyle(
                                        sp: 15.sp, fontFamily: fontMedium),
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toFeedback();
                          },
                        ),
                      ],
                    ),
                  ),
                  LfCategoryWidget(
                    title: strAboutUs.tr,
                    child: Column(
                      children: [
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 40.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    child: Text(
                                  strServiceAgreement.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                )),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toBrowserPage(
                                title: strServiceAgreement.tr,
                                url: '$webUrl$serviceAgreementUrl');
                          },
                        ),
                        _divider(),
                        InkWell(
                          child: Container(
                            constraints: BoxConstraints(minHeight: 40.h),
                            alignment: Alignment.centerLeft,
                            child: Row(
                              children: [
                                Expanded(
                                    child: Text(
                                  strPrivacyPolicy.tr,
                                  style: lfTextStyle(
                                      sp: 15.sp, fontFamily: fontMedium),
                                  overflow: TextOverflow.ellipsis,
                                )),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorFEFEFE,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            toBrowserPage(
                                title: strPrivacyPolicy.tr,
                                url: '$webUrl$privacyPolicyUrl');
                          },
                        ),
                      ],
                    ),
                  ),
                  20.h.sbh,
                  Center(
                    child: InkWell(
                      onLongPress: () {
                        // toLfGridScrollTest();
                        if(appIsDebug) {
                          toSuspendPage();
                        }
                      },
                      child: Text(
                        logic.version.value,
                        style: lfTextStyle(
                            sp: 16.sp,
                            fontFamily: fontBold),
                      ),
                    ),
                  ),
                  20.h.sbh,
                  InkWell(
                    child: Text(
                      strSignOut.tr,
                      style: lfTextStyle(
                          sp: 15.sp,
                          fontFamily: fontBold,
                          decoration: TextDecoration.underline),
                    ),
                    onTap: () {
                      showActionTips(
                        actionButtonTip: strSure.tr,
                        tips:
                            "${strSignOutTip.tr}\n\n${logic.authService.currentAccount!.email}\n",
                        action: () async {
                          logic.authService.loginOut();
                        },
                      );
                    },
                  ),
                  20.h.sbh,
                  InkWell(
                    child: Text(
                      strDisableDeleteAccount.tr,
                      style: lfTextStyle(
                          sp: 15.sp,
                          fontFamily: fontBold,
                          color:color514F62,
                          decoration: TextDecoration.underline),
                    ),
                    onTap: () {
                      toDisableAccount();
                    },
                  ),
                  20.h.sbh,
                ],
              ),
            ),
          ),
        ),
      ),
      onWillPop: () async {
        Get.back();
        return false;
      },
    );
  }

  Widget _divider() {
    return Container(
      height: 0.8.h,
      color: Colors.transparent,
    );
  }

  Widget verificationTip() {
    if (logic.authService.currentAccount!.verified == 1) {
      return Text(
        strVerified.tr,
        style: lfTextStyle(
          sp: 14.sp,
          fontFamily: fontRegular,
        ),
      );
    }
    if (logic.authService.currentAccount!.verified == 2) {
      return Text(
        strPending.tr,
        style: lfTextStyle(
          sp: 14.sp,
          fontFamily: fontRegular,
        ),
      );
    }
    if (logic.authService.currentAccount!.verified == 3) {
      return Text(
        strFailed.tr,
        style: lfTextStyle(
          sp: 14.sp,
          fontFamily: fontRegular,
          color: Colors.red,
        ),
      );
    }
    return const SizedBox.shrink();
  }
}
